﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

namespace Dotnet.Utils.Utility.DrawingUtil
{
    public static class ImageUtil2
    {
        public static Image ToImage(byte[] bytes)
        {
            Image result;
            using (MemoryStream memoryStream = new MemoryStream(bytes))
            {
                result = Image.FromStream(memoryStream);
            }
            return result;
        }

        public static void ZoomAuto(Stream fromFile, string savePath, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            string directoryName = Path.GetDirectoryName(savePath);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            using (Image image = ImageUtil.ZoomAuto(fromFile, targetWidth, targetHeight, imgFormat, watermarkText, watermarkImage))
            {
                image.Save(savePath, imgFormat);
            }
        }

        private static void DrawWatermark(string watermarkText, string watermarkImage, Image initImage)
        {
            if (!string.IsNullOrEmpty(watermarkText))
            {
                using (Graphics graphics = Graphics.FromImage(initImage))
                {
                    Font font = new Font("黑体", 10f);
                    Brush brush = new SolidBrush(Color.White);
                    graphics.DrawString(watermarkText, font, brush, 10f, 10f);
                    graphics.Save();
                }
            }
            if (!string.IsNullOrEmpty(watermarkImage) && File.Exists(watermarkImage))
            {
                using (Image image = Image.FromFile(watermarkImage))
                {
                    if (initImage.Width >= image.Width && initImage.Height >= image.Height)
                    {
                        using (Graphics graphics = Graphics.FromImage(initImage))
                        {
                            ImageAttributes imageAttributes = new ImageAttributes();
                            ColorMap[] map = new ColorMap[]
                            {
                                new ColorMap
                                {
                                    OldColor = Color.FromArgb(255, 0, 255, 0),
                                    NewColor = Color.FromArgb(0, 0, 0, 0)
                                }
                            };
                            imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
                            float[][] array = new float[5][];
                            float[][] array2 = array;
                            int num = 0;
                            float[] array3 = new float[5];
                            array3[0] = 1f;
                            array2[num] = array3;
                            float[][] array4 = array;
                            int num2 = 1;
                            array3 = new float[5];
                            array3[1] = 1f;
                            array4[num2] = array3;
                            float[][] array5 = array;
                            int num3 = 2;
                            array3 = new float[5];
                            array3[2] = 1f;
                            array5[num3] = array3;
                            float[][] array6 = array;
                            int num4 = 3;
                            array3 = new float[5];
                            array3[3] = 0.5f;
                            array6[num4] = array3;
                            array[4] = new float[]
                            {
                                0f,
                                0f,
                                0f,
                                0f,
                                1f
                            };
                            float[][] newColorMatrix = array;
                            ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);
                            imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                            graphics.DrawImage(image, new Rectangle(initImage.Width - image.Width, initImage.Height - image.Height, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
                            graphics.Save();
                        }
                    }
                }
            }
        }

        public static Image ZoomAuto(Stream fromFile, double targetWidth, double targetHeight, ImageFormat imgFormat, string watermarkText, string watermarkImage)
        {
            Image image = Image.FromStream(fromFile, true);
            Image result;
            if ((double)image.Width <= targetWidth && (double)image.Height <= targetHeight)
            {
                DrawWatermark(watermarkText, watermarkImage, image);
                result = image;
            }
            else
            {
                Image image2;
                Graphics graphics;
                Zoom(targetWidth, targetHeight, image, out image2, out graphics);
                graphics.Clear(Color.White);
                graphics.DrawImage(image, new Rectangle(0, 0, image2.Width, image2.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                DrawWatermark(watermarkText, watermarkImage, image2);
                graphics.Dispose();
                image.Dispose();
                result = image2;
            }
            return result;
        }

        private static void Zoom(double targetWidth, double targetHeight, Image initImage, out Image newImage, out Graphics newG)
        {
            double num = (double)initImage.Width;
            double num2 = (double)initImage.Height;
            if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
            {
                if ((double)initImage.Width > targetWidth)
                {
                    num = targetWidth;
                    num2 = (double)initImage.Height * (targetWidth / (double)initImage.Width);
                }
            }
            else if ((double)initImage.Height > targetHeight)
            {
                num2 = targetHeight;
                num = (double)initImage.Width * (targetHeight / (double)initImage.Height);
            }
            newImage = new Bitmap((int)num, (int)num2);
            newG = Graphics.FromImage(newImage);
            newG.InterpolationMode = InterpolationMode.HighQualityBicubic;
            newG.SmoothingMode = SmoothingMode.HighQuality;
        }

        public static Bitmap BuildCheckCodePicture(out string checkCodeString)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Color[] array = new Color[]
            {
                Color.Black,
                Color.Red,
                Color.Blue,
                Color.Green,
                Color.Orange,
                Color.Brown,
                Color.Brown,
                Color.DarkBlue
            };
            string[] array2 = new string[]
            {
                "Times New Roman",
                "MS Mincho",
                "Book Antiqua",
                "Gungsuh",
                "PMingLiU",
                "Impact"
            };
            char[] array3 = new char[]
            {
                '2',
                '3',
                '4',
                '5',
                '6',
                '8',
                '9',
                'A',
                'B',
                'C',
                'D',
                'E',
                'F',
                'G',
                'H',
                'J',
                'K',
                'L',
                'M',
                'N',
                'P',
                'R',
                'S',
                'T',
                'W',
                'X',
                'Y'
            };
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                stringBuilder.Append(array3[random.Next(array3.Length)]);
            }
            checkCodeString = stringBuilder.ToString();
            Bitmap bitmap = new Bitmap(100, 40);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            for (int i = 0; i < 10; i++)
            {
                int x = random.Next(100);
                int y = random.Next(40);
                int x2 = random.Next(100);
                int y2 = random.Next(40);
                Color color = array[random.Next(array.Length)];
                graphics.DrawLine(new Pen(color), x, y, x2, y2);
            }
            for (int i = 0; i < stringBuilder.Length; i++)
            {
                string familyName = array2[random.Next(array2.Length)];
                Font font = new Font(familyName, 18f);
                Color color = array[random.Next(array.Length)];
                graphics.DrawString(stringBuilder[i].ToString(), font, new SolidBrush(color), (float)i * 20f + 8f, 8f);
            }
            for (int i = 0; i < 100; i++)
            {
                int x3 = random.Next(bitmap.Width);
                int y3 = random.Next(bitmap.Height);
                Color color = array[random.Next(array.Length)];
                bitmap.SetPixel(x3, y3, color);
            }
            return bitmap;
        }

        public static Stream BuildCheckCodeStream(out string checkCodeString)
        {
            Bitmap bitmap = ImageUtil.BuildCheckCodePicture(out checkCodeString);
            MemoryStream memoryStream = new MemoryStream();
            try
            {
                bitmap.Save(memoryStream, ImageFormat.Png);
            }
            finally
            {
                bitmap.Dispose();
            }
            return memoryStream;
        }
    }
}